﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using System.Web.UI.HtmlControls;
using System.Text;
using Framework.Library.Core.Utils.GlobalHelper;
using System.IO;
using Framework.Library.Core.Config;
using Framework.Library.Base.Extensions;
using Framework.Library.Base.Helper;

namespace Framework.Portal.Layouts.Admin.Popups.FileManager
{
    public partial class Default : UserControl
    {
        public ConfigHelper cfg
        {
            get { return new ConfigHelper(); }
        }

        protected string uploadPath = string.Empty;
        protected string rootName = string.Empty;
        protected string viewLayout = string.Empty;
        protected string strEditor = string.Empty;
        protected string strSource = string.Empty;
        protected string CKEditorFuncNum = string.Empty;
        protected string returnID = string.Empty;
        protected string extension_whitelist = string.Empty;

        protected void RegisterCss(List<string> infoTheme)
        {
            if (infoTheme != null)
            {
                //Add Style Tag
                foreach (var s in infoTheme)
                {
                    var link = new HtmlLink();
                    link.Attributes.Add("href", Page.ResolveUrl(s));
                    link.Attributes.Add("rel", "stylesheet");
                    link.Attributes.Add("type", "text/css");
                    this.Page.Header.Controls.Add(link);
                    Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));
                }
            }
        }

        protected void RegisterJavascript(List<string> infoTheme)
        {
            if (infoTheme != null)
            {
                //Add Srcipt Tag
                foreach (var s in infoTheme)
                {
                    var javascriptControl = new HtmlGenericControl("script");
                    javascriptControl.Attributes.Add("type", "text/javascript");
                    javascriptControl.Attributes.Add("src", Page.ResolveUrl(s));
                    Page.Header.Controls.Add(javascriptControl);
                    Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));
                }
            }
        }

        protected void RegisterExeJavascript()
        {
            //Add Srcipt Tag
            StringBuilder sb = new StringBuilder();
            sb.Append("var returnID = \"" + returnID + "\";");
            sb.Append("var editor = \"" + strEditor + "\";");
            sb.Append("var source = \"" + strSource + "\";");
            sb.Append("var funcNum = \"" + CKEditorFuncNum + "\";");
            sb.Append("var select_one_file = \"Select only one file to insert!\";");
            sb.Append("var insert_cancelled = \"Insert cancelled because there is no target to insert to!\";");
            sb.Append("var invalid_characters_used = \"Invalid characters used!\";");
            sb.Append("var rename_file = \"Please give a new name for file\";");
            sb.Append("var rename_folder = \"Please give a new name for folder\";");
            sb.Append("var rename_error = \"Rename failed!\";");
            var exeJavascript = new HtmlGenericControl("script");
            exeJavascript.Attributes.Add("type", "text/javascript");
            exeJavascript.InnerHtml = sb.ToString();
            this.Page.Header.Controls.Add(exeJavascript);
            Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                uploadPath = cfg.UploadDir.VALUE + Session.GetUser().USERID + "/" + Configurations.PORTALID + "/";
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadPath)))
                    Directory.CreateDirectory(Server.MapPath(uploadPath));

                rootName = Path.GetFileName(Server.MapPath(uploadPath.TrimEnd('/')));
                if (!string.IsNullOrEmpty(Request.QueryString["editor"]))
                    strEditor = Request.QueryString["editor"];
                if (!string.IsNullOrEmpty(Request.QueryString["source"]))
                    strSource = Request.QueryString["source"];

                string lstFilter = string.Empty;
                if (!string.IsNullOrEmpty(Request.QueryString["filter"]))
                {
                    extension_whitelist = cfg.MediaFilters(Request.QueryString["filter"]).VALUE;
                    switch (Request.QueryString["filter"])
                    {
                        case "flash":
                            lstFilter += "<option value=\"." + cfg.MediaFilters("flash").VALUE.Replace(",", "|.") + "\">Flash&nbsp;</option>";
                            break;
                        case "media":
                            lstFilter += "<option value=\"." + cfg.MediaFilters("media").VALUE.Replace(",", "|") + "\">Media&nbsp;</option>";
                            break;
                        case "image":
                            lstFilter += "<option value=\"." + cfg.MediaFilters("image").VALUE.Replace(",", "|") + "\">Images&nbsp;</option>";
                            break;
                        case "document":
                            lstFilter += "<option value=\"." + cfg.MediaFilters("document").VALUE.Replace(",", "|") + "\">Documents&nbsp;</option>";
                            break;
                        default:
                            lstFilter += "<option value=\"\">All files (*.*)&nbsp;</option>";
                            break;
                    }
                }
                else
                {
                    extension_whitelist = cfg.MediaFilters("all").VALUE;
                    lstFilter += "<option value=\"\">All files (*.*)&nbsp;</option>";
                    lstFilter += "<option value=\"." + cfg.MediaFilters("flash").VALUE.Replace(",", "|") + "\">Flash&nbsp;</option>";
                    lstFilter += "<option value=\"." + cfg.MediaFilters("image").VALUE.Replace(",", "|") + "\">Images&nbsp;</option>";
                    lstFilter += "<option value=\"." + cfg.MediaFilters("media").VALUE.Replace(",", "|") + "\">Media&nbsp;</option>";
                    lstFilter += "<option value=\"." + cfg.MediaFilters("document").VALUE.Replace(",", "|") + "\">Documents&nbsp;</option>";
                }
                lblFilters.Text = lstFilter;
                extension_whitelist = "*." + extension_whitelist.Replace(",", ";*.").Trim();

                if (Request.Cookies["pdw-view"] != null)
                    viewLayout = Request.Cookies["pdw-view"].Value;
                else
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["pdw-view"]))
                        viewLayout = Request.QueryString["pdw-view"];
                    else
                        viewLayout = "content";
                }
                if (!string.IsNullOrEmpty(Request.QueryString["CKEditorFuncNum"]))
                    CKEditorFuncNum = Request.QueryString["CKEditorFuncNum"];
                if (!string.IsNullOrEmpty(Request.QueryString["returnID"]))
                    returnID = Request.QueryString["returnID"];

                RegisterExeJavascript();
                List<string> lstCss = new List<string>();
                lstCss.Add("/Resource/Styles/filemanager/mediabrowser.css");
                RegisterCss(lstCss);
                List<string> lstjs = new List<string>();
                lstjs.Add("/Templates/Admin/Resources/javascript/jquery-1.4.2.js");
                lstjs.Add("/Resource/Javascript/filemanager/jquery.mediabrowser.js");
                lstjs.Add("/Resource/Javascript/filemanager/jquery.plugins.js");
                lstjs.Add("/Resource/Javascript/filemanager/swfupload/swfupload.min.js");
                lstjs.Add("/Resource/Javascript/filemanager/swfupload/plugins/swfupload.queue.js");
                lstjs.Add("/Resource/Javascript/filemanager/swfupload/fileprogress.js");
                lstjs.Add("/Resource/Javascript/filemanager/swfupload/handlers.js");                
                
                if (!string.IsNullOrEmpty(strEditor))
                {
                    if (string.Equals(strEditor, "tinymce", StringComparison.InvariantCultureIgnoreCase))
                        lstjs.Add("/Resource/Javascript/filemanager/tiny_mce_popup.js");
                }
                RegisterJavascript(lstjs);

                lblTreeview.Text = HtmlHelper.RenderTree(uploadPath);
            }
        }
    }
}